package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	widget2 "fyne.io/fyne/v2/widget"
	"image/color"
)

var _ widgetcommon.IWidgetItem = (*Label)(nil)

func init() {
	//widget.RegWidget("WidgetEdit", NewLabel)
	widget.RegWidget("WidgetCheckBox", "文本1", NewLabel)
	widget.RegWidget("WidgetClipper", "文本2", NewLabel)
	widget.RegWidget("WidgetList", "文本3", NewLabel)
	//widget.RegWidget("WidgetTxt", NewLabel)
	widget.RegWidget("WidgetTxtAtlas", "文本4", NewLabel)
	//widget.RegWidget("WidgetNode", NewLabel)

	//widget.RegWidget("Label", NewLabel)
	//
	//widget.RegWidget("ImagicEmpty", NewLabel)
	//widget.RegWidget("TxtEmpty", NewLabel)
	//widget.RegWidget("Empty", NewLabel)
}

type Label struct {
	*WidgetContainer
	Label *widget2.Label
	*Action
	widgetcommon.IUserInfo
	rootNode widgetcommon.IWidgetItem //父节点
}

func NewLabel(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	o := widget2.NewLabel(name)
	_label := &Label{
		Label:  o,
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{211, 211, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_label.WidgetContainer.ac = _label.Action
	_label.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _label)
	_label.WidgetContainer.SetObjects([]fyne.CanvasObject{_label.Label})
	_label.SetBase(_label, rootNode)
	_label.ExtendBaseWidget(_label.Label)
	_label.Label.Text = name
	return _label
}

func (l *Label) SetProperty(key, value string, isProperty bool) {
	l.WidgetContainer.SetProperty(key, value, isProperty)
	switch key {
	case "Scale":
		size := l.Label.Size()
		scale := utils.ParseFloat32(value)
		newSize := fyne.NewSize(size.Width*scale, size.Height*scale)
		l.Label.Resize(newSize)
	case "ShowTxt":
		l.Label.SetText(value)
	case "FontSize":
		//// 重新创建 Label 以改变字体大小
		//newLabel := widget2.NewLabelWithStyle(fmt.Sprintf("更新文本: %d", counter), widget2.TextAlignCenter, widget2.TextStyle{Bold: true})
		//newLabel.TextStyle = widget2.TextStyle{Bold: true}
		//newLabel.Resize(32)
	case "Color":
		//// 转换并设置颜色
		//r.label.Label.TextStyle = fyne.TextStyle{Color: r.label.textColor}
		//col, err := utils.ParseColor(value)
		//if err == nil {
		//	l.Label.TextStyle = fyne.TextStyle{Color: col} // 设置文本颜色
		//	l.Label.Refresh()                              // 刷新标签以应用更改
		//} else {
		//	fmt.Println("Error parsing color:", err)
		//}
	}
}
